package com.javaweb.secondhand_trading.controller;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.javaweb.secondhand_trading.Dto.OrderDto;
import com.javaweb.secondhand_trading.common.BaseResponse;
import com.javaweb.secondhand_trading.entity.Order;
import com.javaweb.secondhand_trading.service.OrderService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/orders")
public class OrderController {
    private final OrderService orderService;

    // 构造器注入 OrderService
    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }


    /**
     * 创建新订单
     * POST /api/orders
     */
    @PostMapping
    public BaseResponse<Order> createOrder(@RequestBody Order order) {
        orderService.createOrder(order);

        return BaseResponse.success("订单创建成功", order);
    }

    /**
     * 更新订单状态
     * PUT /api/orders/{orderId}/status
     */
    @PutMapping("/{orderId}/status")
    public BaseResponse<Order> updateOrderStatus(Order order, @PathVariable("orderId") Integer orderId) {
        orderService.updateOrderStatus(orderId, order.getStatus());
        return BaseResponse.success("订单更新完成", order);
    }

    /**
     * 获取订单详情
     * GET /api/orders/{orderId}
     */
    @GetMapping("/{orderId}")
    public BaseResponse<OrderDto> getOrderDetail(@PathVariable Integer orderId) {
        orderService.getOrderDetail(orderId);
        return BaseResponse.success("订单信息获取成功", orderService.getOrderDetail(orderId));
    }

    /**
     * 获取买家订单列表
     * GET /api/orders/buyer/{buyerId}
     */
    @GetMapping("/buyer/{buyerId}")
    public BaseResponse<List<Order>> getBuyerOrders(
            @PathVariable Integer buyerId,
            @RequestParam(required = false) String status) {

        List<Order> buyerOrders = orderService.getBuyerOrders(buyerId);

        return BaseResponse.success("获取买家订单列表成功",buyerOrders);
    }

    /**
     * 获取卖家订单列表
     * GET /api/orders/seller/{sellerId}
     */
    @GetMapping("/seller/{sellerId}")
    public BaseResponse<List<Order>> getSellerOrders(
            @PathVariable Integer sellerId,
            @RequestParam(required = false) String status) {
        List<Order> sellerOrders = orderService.getSellerOrders(sellerId);
        return BaseResponse.success("获取卖家订单列表成功",sellerOrders);
    }


    @DeleteMapping("{orderId}")
    public BaseResponse deleteOrders(@RequestBody Integer orderId) {
        orderService.deleteOrder(orderId);
        return BaseResponse.success("订单删除成功");
    }

}
